﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ViewMaker.Core.Utils;
using System.ComponentModel;
using ViewMaker.Core.Controls;
using System.Windows.Input;
using System.IO;
using System.Xml.Serialization;
using System.ComponentModel.DataAnnotations;

namespace ViewMaker.Core.ViewModels
{
    [View(ViewControlType.Grid, Title = "Layout List")]
    [ViewProperty(GridViewControl.Properties.Margin, "10")]
#if !SILVERLIGHT
    [ViewProperty(GridViewControl.Properties.WindowWidth, 600)]
    [ViewProperty(GridViewControl.Properties.WindowHeight, 300)]
#else
    [ViewProperty(GridViewControl.Properties.Width, 600)]
    [ViewProperty(GridViewControl.Properties.Height, 300)]
#endif
    public class ViewLayoutDataViewModel : ViewModelBase
    {
        protected IViewLayoutPersistenceService ViewLayoutPersistence
        {
            get { return ServiceLocator.GetService<IViewLayoutPersistenceService>(); }
        }

        [View(ViewControlType.ListBox)]
        [ViewProperty(ListBoxViewControl.Properties.ItemsSource, "LayoutDataList")]
        [ViewProperty(ListBoxViewControl.Properties.GridHeight, "*")]
        [ViewProperty(ListBoxViewControl.Properties.VerticalAlignment, LayoutVerticalAlignment.Stretch)]
        [ViewProperty(ListBoxViewControl.Properties.HeaderPosition, LayoutHeaderPosition.Hidden)]
        public string CurrentData { get; set; }

        [Browsable(false)]
        public List<string> LayoutDataList
        {
            get { return _layoutDataList; }
            set { _layoutDataList = value; OnPropertyChanged("LayoutDataList"); }
        }
        private List<string> _layoutDataList;

        public ViewLayoutDataViewModel()
        {
            LayoutDataList = ViewLayoutPersistence.ListData();
        }

        [Display(Name = "Show")]
        public ICommand ShowCommand { get { return this.CreateCommand(ShowAction); } }
        private void ShowAction()
        {
            if (CurrentData == null) return;
            using (var st = new StringWriter())
            {
                var ser = new XmlSerializer(typeof(ViewLayoutItem));
                ser.Serialize(st, ViewLayoutPersistence.Load(CurrentData));
                ViewUtil.ShowDialog(new CodeViewModel { Code = st.ToString() });
            }
        }

        [Display(Name = "Save As")]
        public ICommand SaveAsCommand { get { return CreateCommand(SaveAsAction); } }
        private void SaveAsAction()
        {
            if (CurrentData == null) return;
            using (var wr = this.ShowSaveFile("(*.xml)|*.xml"))
            {
                if (wr != null)
                {
                    ViewLayoutPersistence.Serialize(ViewLayoutPersistence.Load(CurrentData), wr);
                }
            }
        }

        [Display(Name = "Delete")]
        public ICommand DeleteComamnd { get { return this.CreateCommand(DeleteAction); } }
        private void DeleteAction()
        {
            if (CurrentData == null) return;
            ViewLayoutPersistence.Delete(CurrentData);
            LayoutDataList = ViewLayoutPersistence.ListData();
        }

        [Display(Name = "Close")]
        public ICommand CloseCommand { get { return CreateViewCommand(ViewCommands.Close); } }


    }
}
